/**
 * 
 */
package handwritten.gesture;




import handwritten.character.HandWrittenCharacter;
import handwritten.character.RawHandWrittenCharacter;
import handwritten.grid.InputGrid;
import handwritten.grid.SingleCellTrainning;
import handwritten.grid.Trainning;

import java.util.ArrayList;

import utility.math.Translation;
import utility.math.Vector2D;

/**
 * Singleton instance of GestureManager
 * @author tioz
 *
 */
public class HandInputManager {

	
	static private HandInputManager instance;
	
	//Manage the area where the text is written
	private InputGrid grid;
	private Trainning trainningInput;
	
	private ArrayList<MultiGesture> letters;
	private Gesture gestureCandidate;


	
	/**
	 * 
	 */
	protected HandInputManager() {
		gestureCandidate = new Gesture();
		letters = new ArrayList<MultiGesture>();
		//grid = new BasicInputGrid(2,60,60,20);
		grid = new SingleCellTrainning(60,60,20);
		trainningInput = (Trainning) grid;
	}

	public Gesture getGestureCandidate() {
		return gestureCandidate;
	}


	public static HandInputManager getInstance(){
		if(instance == null){
			instance = new HandInputManager();
		}
		return instance; 
	}
	
	/**
	 * 
	 */
	
	public void beginGesture(int x,int y){
		
		boolean triggerdetection= beginLetter(new Vector2D(x, y));
		
		if(triggerdetection)
		{
			triggerDetection();
			System.out.println("detect trigger");
		}
		
		gestureCandidate= new Gesture();
		gestureCandidate.appendToPath(new Vector2D(x,y));
	}
	
	public void continueGesture(int x,int y){
		Vector2D nextElement= new Vector2D(x,y);
		if(!gestureCandidate.endOfPath().equals(nextElement)) {
			gestureCandidate.appendToPath(nextElement);
		} 
	}
	
	public void endGesture(){
		letters.get(letters.size()-1).add(gestureCandidate);
	}
	
	public void clearGesture(){
		letters.clear();
		gestureCandidate = new Gesture();
	}
	
	/**
	 * @param pos
	 * @return if a new move is started
	 */
	public boolean beginLetter(Vector2D pos){
		boolean moveOver = false;
		
		if(letters.size()== 0 || !(grid.getCellBoundary(letters.get(letters.size()-1).startOfLetter())[0].equals( grid.getCellBoundary(pos)[0])))
		{
			if(letters.size()!= 0)
			{
				moveOver=true;
				//change the coordiate sytem of the letter
				//letters.get(letters.size()-1).translate(grid.getCellTopLeft(letters.get(letters.size()-1).startOfLetter()).time(-1));
				letters.get(letters.size()-1).transform(new Translation(grid.getCellTopLeft(letters.get(letters.size()-1).startOfLetter()).time(-1)));
			}
			letters.add(new MultiGesture());
		}
		return moveOver;
	}		
	

	public InputGrid getInputGrid(){
		return grid;
	}

	public ArrayList<MultiGesture> getLetters(){
		return letters;
	}
	
	
	public void triggerDetection(){
		MultiGesture  gest = letters.get(letters.size()-2);
		int cell[][]=new int[grid.getTextCellWidth()][grid.getTextCellHeight()];
		
		gest.fillPath(cell);
		
		for(int j=0;j< cell[0].length;j++)
		{
			for(int i=0;i< cell.length;i++)
			{
				System.out.print(cell[i][j]);
			}
			System.out.println();
		}
		
		System.out.println("Left is: "+gest.left().getX()+" Rigth is: "+gest.right().getX()+  "Width is: "+gest.width());
		System.out.println("center of masse X: "+gest.centerOfMass().getX()+" Y: "+gest.centerOfMass().getY());
		System.out.println("top is: "+gest.top().getY()+" bottom is: "+gest.bottom().getY() +  "Width is: "+gest.height());
		System.out.println("legnth: "+gest.pathLength());

		

		
		
		cell=new int[grid.getTextCellWidth()][grid.getTextCellHeight()];
		gest.fillPath(cell);
		
		for(int j=0;j< cell[0].length;j++)
		{
			for(int i=0;i< cell.length;i++)
			{
				System.out.print(cell[i][j]);
			}
			System.out.println();
		}
		for(Gesture g : gest.getCollection())
		{
			System.out.println("number of point: "+g.nPoint());
		}
		
		/*
		 * testing purpose
		 */
		
		new HandWrittenCharacter("testx",letters.get(letters.size()-2));
		
	}
	//FIXME Effet de bord joli. get == get and erase...
	public RawHandWrittenCharacter getHandWrittenCharacter(){
		//take top of grid has start
		//letters.get(letters.size()-1).translate(grid.getCellTopLeft(letters.get(letters.size()-1).startOfLetter()).time(-1));
		letters.get(letters.size()-1).transform(new Translation(grid.getCellTopLeft(letters.get(letters.size()-1).startOfLetter()).time(-1)));
		
		MultiGesture  gest = letters.get(letters.size()-1);
		
		
		RawHandWrittenCharacter hwChar = new RawHandWrittenCharacter(
				trainningInput.expectedChar(grid.getCellNumber(gest.startOfLetter())),gest);
		
		return hwChar;
	}
	
	public Trainning getTrainning(){
		return trainningInput;
	}

	public void inputDeviceResized(int width, int height) {
		getInputGrid().update(0,0,width,height);   
		
	}	
}
